home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Testing & Debugging / Heap Purge dcmd / hp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.8 KB  |  169 lines  |  [TEXT/MPS ]

  1. /*  HP.c
  2.   This is the Heap Purge dcmd.
  3.  
  4.   The following MPW commands will build the dcmd and copy it to the
  5.   "Debugger Prefs" file in the System folder. The dcmd's name in
  6.   MacsBug will be the name of the file built by the Linker.
  7.   You must first copy dcmd.h, put.h, dcmdGlue.a.o, put.c.o, and DRunTime.o from the
  8.   dcmds folder into this folder.
  9.  
  10.   Asm hp.a
  11.   C -r -b hp.c #-sym on,novars,notypes -mbg off # for UltraSlimFast
  12.   Link dcmdGlue.a.o hp.c.o hp.a.o put.c.o DRuntime.o "{Libraries}"Interface.o -o hp
  13.   BuildDcmd hp 1235
  14.   #Echo 'include "hp";'  |  Rez -a -o "{SystemFolder}TMON Folder:DCMD Holder"
  15.   Echo 'include "hp";'   |  Rez -a -o "{SystemFolder}Debugger Prefs"
  16.  
  17.   DumpObj hp.c.o > hp.dumpObj
  18.   UltraSlimFast hp.dumpObj >hp.UltraSlimFast
  19.  
  20. */
  21.  
  22. #include <Types.h>
  23. #include <Memory.h>
  24. #include <OSUtils.h>
  25. #include <Files.h>
  26. #include <Menus.h>
  27. #include <Traps.h>
  28.  
  29. #include "dcmd.h"
  30. #include "put.h"
  31.  
  32. extern pascal void NewNewPtr();
  33. extern pascal void NewNewHandle();
  34. extern pascal void NewReallocHandle();
  35. extern pascal void NewSetPtrSize();
  36. extern pascal void NewSetHandleSize();
  37. extern pascal void NewMoveHHi();
  38. extern pascal void SaveOldTrapAddress (long address, short addressKind);
  39. extern pascal void SaveMyA5();
  40. extern pascal long GetMyA5();
  41.  
  42. #define kOldNewPtr           0
  43. #define kOldNewHandle        1
  44. #define kOldReallocHandle    2
  45. #define kOldSetPtrSize       3
  46. #define kOldSetHandleSize    4
  47. #define kOldMoveHHi          5
  48.  
  49. #define sysZoneMask          0x0400
  50.  
  51. Str255        pDumpString;
  52. char          ch;
  53. Boolean       purgingOn;
  54.  
  55.  
  56. pascal void PurgeIt(long blockAddress, long blockLength, long addrOfMasterPtr,
  57.                     short blockType, Boolean locked, Boolean purgeable, 
  58.                     Boolean resource)
  59. {
  60. #pragma unused (blockAddress, blockLength, addrOfMasterPtr, resource)
  61.  
  62.     if ((blockType == relocatableBlock) && (!locked) && (purgeable)) {
  63.         EmptyHandle((Handle)addrOfMasterPtr);
  64.     }
  65. }
  66.  
  67.  
  68. Boolean    IsSysZone(unsigned short TrapWord)
  69. {
  70.     return( (TrapWord & sysZoneMask) != 0);
  71. }
  72.  
  73.  
  74. pascal void PurgeAllBlocks(unsigned short TrapWord)
  75. {
  76.     THz     oldZone;
  77.     long    oldA5;
  78.     
  79.     oldA5 = SetA5(GetMyA5());
  80.     if (purgingOn) {
  81.         if (IsSysZone(TrapWord)) {
  82.             dcmdSwapWorlds();
  83.             oldZone = GetZone();
  84.             SetZone(SystemZone());
  85.             dcmdSwapWorlds();
  86.         }
  87.     
  88.         dcmdForAllHeapBlocks(PurgeIt);
  89.     
  90.         if (IsSysZone(TrapWord)) {
  91.             dcmdSwapWorlds();
  92.             SetZone(oldZone);
  93.             dcmdSwapWorlds();
  94.         }
  95.     }
  96.     (void) SetA5(oldA5);
  97. }
  98.  
  99. TrapType GetTrapType(short theTrap)
  100. {
  101.     // OS traps start with A0, Tool with A8 or AA. 
  102.     return((theTrap & 0x0800) ? ToolTrap : OSTrap);
  103. }
  104.  
  105. void PatchTrap(short trapNumber, short saveOffset, long newAddress)
  106. {
  107.     // Use NGetTrapAddress since it is always safer on current machines. Take 
  108.     // the result it gives me, and save it off in asm land, for future 
  109.     // reference. Then, move in the new address of the routine, my asm glue. 
  110.     
  111.     SaveOldTrapAddress(NGetTrapAddress(trapNumber, GetTrapType(trapNumber)), 
  112.                        saveOffset);
  113.     NSetTrapAddress(newAddress, trapNumber, OSTrap);    
  114. }
  115.  
  116. void InstallPatches()
  117. {
  118.     // Patch the traps… These are being patched in the world, not in the 
  119.     // debugger world. Switch over to the real world, in case the debugger 
  120.     // does world swaps. TMon Pro.
  121.  
  122.     dcmdSwapWorlds();
  123.  
  124.     PatchTrap(_NewPtr, kOldNewPtr, (long) NewNewPtr);
  125.     PatchTrap(_NewHandle, kOldNewHandle, (long) NewNewHandle);
  126.     PatchTrap(_ReallocHandle, kOldReallocHandle, (long) NewReallocHandle);
  127.     PatchTrap(_SetPtrSize, kOldSetPtrSize, (long) NewSetPtrSize);
  128.     PatchTrap(_SetHandleSize, kOldSetHandleSize, (long) NewSetHandleSize);
  129.     PatchTrap(_MoveHHi, kOldMoveHHi, (long) NewMoveHHi);
  130.  
  131.     // Switch back to debugger world.
  132.     dcmdSwapWorlds();
  133. }
  134.  
  135.  
  136. pascal void CommandEntry(dcmdBlock* paramPtr)
  137. {
  138.     switch (paramPtr->request)
  139.         {
  140.         case dcmdInit:
  141.             SaveMyA5();
  142.             purgingOn = false;
  143.             InstallPatches();
  144.             break;
  145.  
  146.         case dcmdHelp:
  147.             dcmdDrawLine("\php");
  148.             dcmdDrawLine("\p   Toggle Heap Purge on and off. When on, heap purge purges purgable blocks");
  149.             dcmdDrawLine("\p   on NewPtr, NewHandle, ReallocHandle, SetPtrSize, SetHandleSize, and MoveHHi");
  150.             dcmdDrawLine("\p   calls.");
  151.             break;
  152.  
  153.         case dcmdDoIt:
  154.             dcmdDrawLine("\pHeap purge is ");
  155.             if (purgingOn)
  156.                 dcmdDrawString("\poff.");
  157.             else
  158.                 dcmdDrawString("\pon.");
  159.             purgingOn = !purgingOn;
  160.             break;
  161.  
  162.         default:
  163.             PutPStr("\pUnknown request ");
  164.             PutUDec(paramPtr->request);
  165.             PutLine();
  166.             break;
  167.         }
  168. } // CommandEntry
  169.